-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local DiesalGUI 	= LibStub('DiesalGUI-1.0')
local DiesalStyle = LibStub("DiesalStyle-1.0")
local DiesalTools = LibStub('DiesalTools-1.0')
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local tostring										= tostring
local format 										= string.format
-- ~~| WoW Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~| ConfigToggle |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local Type 		= 'OPTinput'
local Version 	= 1
-- ~~| ConfigToggle StyleSheets |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local styleSheet = {
	['name-color'] = {
		type			= 'Font',
		color			= 'bfbfbf',
	},
	['input-background'] = {	
		type			= 'texture',
		layer			= 'BACKGROUND',
		color			= '000000',
		offset		= 0,		
	},
	['input-gradient'] = {			
		type			= 'texture',
		layer			= 'BORDER',
		gradient		= 'VERTICAL',							
		color			= 'ffffff',	
		alpha 		= 0,
		alphaEnd		= .1,
		offset		= -1,		
	},
	['input-outline'] = {		
		type			= 'outline',
		layer			= 'BORDER',	
		color			= '000000',
		offset		= 0,		
	},	
	['input-inline'] = {		
		type			= 'outline',
		layer			= 'BORDER',
		gradient		= 'VERTICAL',	
		color			= 'ffffff',
		alpha 		= .02,
		alphaEnd		= .05,
		offset		= -1,	
	},	
	['input-hover'] = {		
		type			= 'outline',
		layer			= 'HIGHLIGHT',
		color			= 'ffffff',
		alpha 		= .12,	
		offset		= -1,	
	},		
}
-- ~~| ConfigToggle Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~| ConfigToggle Methods |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local methods = {
	['OnAcquire'] = function(self)
		self:AddStyleSheet(styleSheet)
		-- self:AddStyleSheet(wireFrameSheet)
		self:Show()
		self.checkBox:Hide()
	end,
	['OnRelease'] = function(self)

	end,
	['ApplySettings'] = function(self)
		local optionTable = self.settings.optionTable
		self:SetHeight(18)
		if self.settings.position == 1 then
			self:SetPoint('TOPLEFT',0,-self.settings.padTop)
			self:SetPoint('RIGHT')
		else
			self:SetPoint('TOPLEFT',self.settings.parentObject.children[self.settings.position-1].frame,'BOTTOMLEFT',0,0)
			self:SetPoint('RIGHT')
		end

		if optionTable.toggle ~= false then self.checkBox:Show() end	
		self.name:SetText(optionTable.name)			
		self.input:SetWidth(optionTable.width and DiesalTools:Round(optionTable.width,2)-1 or 50)			
	end,
	['SetDB'] = function(self,db,moduleDB)
		self.settings.db 			= db
		self.settings.moduleDB 	= moduleDB

		if self.settings.optionTable.toggle ~= false then
			self.checkBox:SetChecked(db.enable)
			if self.settings.optionTable.set then self.settings.optionTable.set('toggle',db.enable) end
			self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'toggle',db.enable)
		end
		
		self.input:SetText(db.value or '')
		self.input:SetCursorPosition(0)
		if self.settings.optionTable.set then self.settings.optionTable.set('input',db.value) end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'input',db.value)		
	end,
	['Lock'] = function(self,lock)
		self.checkBox:Disable()
		self.input:EnableMouse(false)
	end,
	['Unlock'] = function(self,lock)
		self.checkBox:Enable()
		self.input:EnableMouse(true)
	end,
}
-- ~~| ConfigToggle Constructor |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local function Constructor()
	local self 		= DiesalGUI:CreateObjectBase(Type)
	local frame		= CreateFrame('Frame',nil,UIParent)
	self.frame		= frame
	-- ~~ Default Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.defaults = { }
	-- ~~ Registered Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	-- ~~ Construct ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	local checkBox = DiesalGUI:Create('CheckBox')
	checkBox:SetParent(frame)
	checkBox:SetPoint("TOPLEFT",2,-3)
	checkBox:SetEventListener('OnValueChanged', function(this,event,value)
		self.settings.db.enable = value
		if self.settings.optionTable.set then self.settings.optionTable.set('toggle',value)	end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'toggle',value)
	end)
	checkBox:SetEventListener('OnEnter', function(this)
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(checkBox.frame, "ANCHOR_TOPLEFT",0,2)
		GameTooltip:AddLine('enable option')
		GameTooltip:Show()
	end)
	checkBox:SetEventListener('OnLeave', function(this)
		GameTooltip:Hide()
	end)
	
	local input = self:CreateRegion("EditBox", 'input', frame)	
	input:SetHeight(16)	
	input:SetPoint('TOPRIGHT',-3,-1)	
	input:SetAutoFocus(false)			
	input:SetJustifyH("CENTER")
	input:SetJustifyV("CENTER")	
	input:SetTextInsets(1,0,2,0)
	input:SetTextColor(.8,.8,.8,1)		
	input:SetScript('OnEnterPressed',  function(this,...)				
		if this:GetText() ~= self.settings.oldInput then
			self.settings.oldInput = this:GetText()	
			self.settings.db.value = this:GetText()			
			if self.settings.optionTable.set then self.settings.optionTable.set('input',this:GetText()) end
			self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'input',this:GetText())			
		end
		DiesalGUI:ClearFocus()		
	end)				
	input:HookScript('OnEscapePressed', function(this,...)				
		this:SetText(self.settings.oldInput)		
	end)
	input:HookScript('OnEditFocusLost', function(this,...)				
		this:SetText(self.settings.oldInput)		
	end)	
	input:HookScript('OnEditFocusGained', function(this)				
		self.settings.oldInput = this:GetText()			
	end)	
	input:SetScript("OnEnter",function(this,...)		
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(input, "ANCHOR_TOPRIGHT",0,2)
		GameTooltip:AddLine('Click to edit.')
		GameTooltip:Show()		
	end)
	input:SetScript("OnLeave", function(this,...)		
		GameTooltip:Hide()				
	end)		

	local nameFrame = self:CreateRegion("Frame", 'nameFrame', frame)
	nameFrame:SetHeight(14)
	nameFrame:SetPoint("TOPLEFT",16,-2)
	nameFrame:SetPoint("RIGHT",input,'LEFT',-4,0)
	nameFrame:SetScript('OnEnter', function(this)
		if not self.settings.moduleDB.configTooltips then return end
		if not self.settings.optionTable.tooltip then return end
		GameTooltip:SetOwner(nameFrame, "ANCHOR_TOPLEFT",0,2)
		GameTooltip:AddLine('|cffff7f00'..self.settings.optionTable.tooltip,nil,nil,nil,1)
		GameTooltip:Show()
	end)
	nameFrame:SetScript('OnLeave', function(this)
		GameTooltip:Hide()
	end)

	local name = self:CreateRegion("FontString", 'name', nameFrame)
	name:SetPoint("TOPLEFT",0,-2)
	name:SetPoint("BOTTOMRIGHT",4,-2)
	name:SetJustifyV("TOP")
	name:SetJustifyH("LEFT")
	name:SetWordWrap(false)

	-- ~~ Frames ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.checkBox 	= checkBox
	self.input		= input
	-- ~~ Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	for method, func in pairs(methods) do
		self[method] = func
	end
	-- ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	DiesalGUI:RegisterObject(self)
	return self
end
DiesalGUI:RegisterObjectConstructor(Type,Constructor,Version)